package com.android.UrbanRemix;

/**
 * Reads compass information to generate sound 'virtual path' in direction user faces.
 * @author Ryan
 * 
 */
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Timer;
import java.util.TimerTask;
import java.lang.Runnable;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Config;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.TextView;

import android.widget.ViewSwitcher;

import android.media.AudioManager;
import android.net.Uri;

public class Compass extends Activity implements ViewSwitcher.ViewFactory,
		Runnable {
	private static final String TAG = "Compass";

	private SensorManager mSensorManager;
	private float[] mValues;
	// private TextSwitcher mSwitcher;
	public float calcValue;
	public float calcValue2;
	public static Double lat;
	public static Double lng;
	public static Double lat2;
	public static Double lng2;
	public static int prj;
	public static int zoom;
	public MediaPlayer mPlayer;
	static Timer t = new Timer();
	public int direction = 0;
	public static boolean timer;
	public boolean newCompass = true;
	public Thread compassThread;

//	make new request for sound from AndroidServlet
	public void updateSound() {

		// Make double precision data into integers for html request
		int latInt = (int) (lat * 1E6);
		int lngInt = (int) (lng * 1E6);
		int latInt2 = (int) (lat2 * 1E6);
		int lngInt2 = (int) (lng2 * 1E6);

		String LatStart = Integer.toString(latInt);
		String LngStart = Integer.toString(lngInt);
		String LatEnd = Integer.toString(latInt2);
		String LngEnd = Integer.toString(lngInt2);
	

		// String direction = Integer.toString((int) mValues[0]);

		//    	
//		String URLString = String
//				.format("http://128.61.127.226:8080/AndroidServlet?param1=PathRender&LatStart="
//						+ LatStart
//						+ "&LngStart="
//						+ LngStart
//						+ "&LatEnd="
//						+ LatEnd + "&LngEnd=" + LngEnd);

//		format call as string
		String URLString = String
		.format
		("http://urbanremix.lcc.gatech.edu:8080/urbanremix-webapp/AndroidServlet?param1=PathRender&LatStart="
				+ LatStart
				+ "&LngStart="
				+ LngStart
				+ "&LatEnd="
				+ LatEnd 
				+ "&LngEnd=" 
				+ LngEnd
				+ "&Project="
				+ prj);


		Log.i(TAG, URLString);

		URL androidServletURL;
		try {
//			make call
			androidServletURL = new URL(URLString);
			URLConnection connection = androidServletURL.openConnection();
			InputStream is = connection.getInputStream();

			BufferedReader in = new BufferedReader(new InputStreamReader(is));

			Log.i(TAG, "Buffer");
//			read in address of new sound
			String soundPath2 = in.readLine();

//			String soundPath = String.format("http://128.61.114.194:8080/"
					//+ soundPath2);
			 String soundPath = String.format("http://urbanremix.lcc.gatech.edu:8080/urbanremix-webapp/" +
			 soundPath2);

			URL soundPathUrl = new URL(soundPath);

			Log.i(TAG, "URL");
			Log.i(TAG, soundPath);
//			play new sound
			mediaPlayer(soundPathUrl);
		}

		catch (MalformedURLException e) {
			e.printStackTrace();

		} catch (IOException e) {
			e.printStackTrace();
		}

	}
//	play sound on droid
	public void mediaPlayer(URL soundPathUrl) {

		Log.i(TAG, soundPathUrl.toString());

		try {
			mPlayer = new MediaPlayer();
			mPlayer.reset();
			mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			mPlayer.setDataSource(soundPathUrl.toString());
			mPlayer.prepare();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		mPlayer.start();

	}
//	data in from map.java
	public static void setData(double latitude, double longitude,
			double latitude2, double longitude2) {
		lat = latitude;
		lng = longitude;
		lat2 = latitude2;
		lng2 = longitude2;

	}

	public View makeView() {
		// TODO Auto-generated method stub
		return null;
	}

	public void run() {
		updateSound();
	}

	public void newThread() {
		compassThread = new Thread(this);
		compassThread.start();

	}

}
